home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / LIB / CALCHARM.C < prev    next >
C/C++ Source or Header  |  1995-11-11  |  1KB  |  45 lines

  1. /* ============ */
  2. /* CalcHarm.c    */
  3. /* ============ */
  4. #include <miscdefs.h>
  5. #include <mconf.h>
  6. /* ==================================================================== */
  7. /* CalcHarm - Returns Harmonic corresponding to Power R and Term N    */
  8. /* ==================================================================== */
  9. double
  10. CalcHarm(int R, UINT N)
  11. {
  12.     UINT    k;
  13.     double  RetVal = 0;
  14.  
  15.     /* ------------------------------------- */
  16.     /* Calculate and Add Fractions of Series */
  17.     /* ------------------------------------- */
  18.     for (k = N; k >= 2; --k)
  19.     {
  20.     double    NewDenom = (R == 1) ? k : powi(k, R);
  21.  
  22.     RetVal += 1.0/NewDenom;
  23.     }
  24.  
  25.     return (1.0 + RetVal);
  26. }
  27. # if defined(TEST_HARM)
  28. #include <miscdefs.h>
  29. void
  30. main()
  31. {
  32.     while (main)
  33.     {
  34.     UINT    N;
  35.     int    R;
  36.     double    Value;
  37.  
  38.     GetInt("Enter Power of Harmonic Series [>=1]: ", &R);
  39.     GetUint("Enter Number of Terms in Series: ", &N);
  40.     Value = CalcHarm(R, N);
  41.     printf("Harmonic = %.15e\n", Value);
  42.     }
  43. }
  44. # endif
  45.